package com.jixiantubu.denglu;

import org.apache.http.Header;

import com.example.jixiantubu.R;
import com.jixiantubu.index.MainActivity;
import com.jixiantubu.jinnang.JinNang_YouJi_Find;
import com.jixiantubu.util.CustomProgressDialog;
import com.jixiantubu.util.MyApplication;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;

import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Login extends Activity implements OnClickListener{
	EditText usernameEditText, passwordEditText;
	String usernameString, passwordsString;
	MyApplication myApplication;
	CheckBox checkBox;
	SharedPreferences preferences;
	Editor editor;
TextView  forget;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.login);
		usernameEditText = (EditText) findViewById(R.id.editText1);
		passwordEditText = (EditText) findViewById(R.id.editText2);
		forget=(TextView) findViewById(R.id.login_forget);
		forget.setOnClickListener(this);
		checkBox = (CheckBox) findViewById(R.id.checkBox1);
		preferences = getSharedPreferences("userinfo", MODE_PRIVATE);
		editor = preferences.edit();
		String name = preferences.getString("user_name", "");
		//String password = preferences.getString("user_password", "");
		if (name == null ) {
			checkBox.setChecked(false);
		} else {
			checkBox.setChecked(true);
			usernameEditText.setText(name);
			//passwordEditText.setText(password);
		}
	}
	public void jump(View view) {
		Intent intent = new Intent(Login.this,
				IphoneNumber.class);
		intent.putExtra("1", "1");
		startActivity(intent);
		
		
	}
	

	@SuppressWarnings("static-access")
	public void denglu(View view) {
		usernameString = usernameEditText.getText().toString().trim();
		passwordsString = passwordEditText.getText().toString().trim();
		myApplication = new MyApplication();
		String a = usernameString;
		myApplication.username = a;
		final CustomProgressDialog pd = new CustomProgressDialog(
				Login.this, "正在拼命加载数据,请稍等！", R.anim.frame);
		pd.show();
		MyApplication.httpclient.configSoTimeout(5000);
		AsyncHttpClient client = new AsyncHttpClient();
		String url = "http://" + MyApplication.getIP()
				+ ":8080/Jinnang/Login?user_name=" + usernameString
				+ "&user_password=" + passwordsString;
		client.get(url, new AsyncHttpResponseHandler() {
			@Override
			public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
				// 访问服务器成功
				myApplication.username=usernameString ;
				pd.dismiss();
				String text = new String(arg2);
				Log.e("aa", text);
				if (!text.equals("0")) {
					if (checkBox.isChecked()) {
						editor.putString("user_name", usernameString);
						//editor.putString("user_password", passwordsString);
						
						myApplication.user_id = text;
						editor.commit();
					} else {
						editor.remove("user_name");
						//editor.remove("user_password");
						editor.commit();
					}
					runOnUiThread(new Runnable() {
						public void run() {
							Intent intent = new Intent(Login.this,
									MainActivity.class);
							startActivity(intent);

						}
					});
				} else {

					runOnUiThread(new Runnable() {
						public void run() {
							Toast.makeText(Login.this,
									"Sorry,your name or password is wrong!", Toast.LENGTH_SHORT)
									.show();

						}
					});
				}

			}

			@Override
			public void onFailure(int arg0, Header[] arg1, byte[] arg2,
					Throwable arg3) {
				// 访问服务器失败
				pd.dismiss();
				MyApplication.show(Login.this, "加载数据失败，请检查网络!");
			}
		});
	}
	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.login_forget:
			Intent intent = new Intent(Login.this,
					IphoneNumber.class);
			intent.putExtra("1", "2");
			
			startActivity(intent);
			
			break;

		default:
			break;
		}
		
	}
}
